package com.ruoyi.ppt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ppt.domain.PptContent;
import com.ruoyi.ppt.domain.PptTemplate;
import com.ruoyi.ppt.domain.vo.PptTemplateVO;
import com.ruoyi.ppt.mapper.PptContentMapper;
import com.ruoyi.ppt.mapper.PptTemplateMapper;
import com.ruoyi.ppt.service.PptTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李志鑫
 * @since 2024-07-03
 */
@Service
public class PptTemplateServiceImpl extends ServiceImpl<PptTemplateMapper, PptTemplate> implements PptTemplateService {
    @Autowired
    private PptTemplateMapper pptTemplateMapper;
    @Autowired
    private PptContentMapper pptContentMapper;

    @Override
    public AjaxResult selectList(Integer pageNum, Integer pageSize, Integer templateType, String message) {
        Long userId = SecurityUtils.getUserId();
        Page<PptTemplate> pptTemplatePage = new Page<>(pageNum, pageSize);
        QueryWrapper<PptTemplate> pptTemplateQueryWrapper = new QueryWrapper<>();
        if (null!=templateType&&!templateType.equals("")&&templateType==1) {
            pptTemplateQueryWrapper.eq("user_id",userId);
        }
        if (null!=message&&!message.equals("")) {
            pptTemplateQueryWrapper.like("template_name",message);
        }
        pptTemplateQueryWrapper.orderByDesc("id");
        pptTemplateQueryWrapper.orderByDesc("template_num");

        Page<PptTemplate> pptTemplatePage1 = pptTemplateMapper.selectPage(pptTemplatePage, pptTemplateQueryWrapper);
        List<PptTemplate> records = pptTemplatePage1.getRecords();
        List<PptTemplateVO> pptTemplateVOS = new ArrayList<>();
        for (PptTemplate record : records) {
            PptTemplateVO pptTemplateVO = new PptTemplateVO();
            BeanUtils.copyProperties(record,pptTemplateVO);
            QueryWrapper<PptContent> pptContentQueryWrapper = new QueryWrapper<>();
            List<PptContent> pptContents = pptContentMapper.selectList(pptContentQueryWrapper);
            if (pptContents.size()>0) {
                List<PptContent> pptContents1 = new ArrayList<>();
                pptContents1.add(pptContents.get(0));
                pptTemplateVO.setPptContent(pptContents1);
            }
            pptTemplateVOS.add(pptTemplateVO);
        }
        Page<PptTemplateVO> pptTemplateVOPage = new Page<>();
        pptTemplateVOPage.setCurrent(pptTemplatePage1.getCurrent());
        pptTemplateVOPage.setSize(pptTemplatePage1.getSize());
        pptTemplateVOPage.setTotal(pptTemplatePage1.getTotal());
        pptTemplateVOPage.setRecords(pptTemplateVOS);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",pptTemplateVOPage);
    }

    @Transactional
    @Override
    public AjaxResult addPpt(PptTemplateVO pptTemplateVO) {
        Long userId = SecurityUtils.getUserId();
        PptTemplate pptTemplate = new PptTemplate();
        pptTemplate.setCreateId(userId);
        pptTemplate.setType(1);
        pptTemplate.setUserId(userId);
        BeanUtils.copyProperties(pptTemplateVO,pptTemplate);
        int insert = pptTemplateMapper.insert(pptTemplate);
        if (insert == 0) {
            throw new ServiceException("添加失败");
        }
        for (PptContent pptContent : pptTemplateVO.getPptContent()) {
            int insert1 = pptContentMapper.insert(pptContent);
            if (insert1 == 0) {
                throw new ServiceException("添加失败");
            }
        }
        return new AjaxResult(HttpStatus.SUCCESS,"添加成功");
    }
}
